package com.bs.teachassistant.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bs.teachassistant.R;
import com.bs.teachassistant.common.Comm;
import com.bs.teachassistant.entity.Less;
import com.bs.teachassistant.entity.ScheBean;
import com.bs.teachassistant.utils.FormatUtils;
import com.bs.teachassistant.utils.GsonUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by limh on 2020/4/27.
 * 添加备课信息
 */
@ContentView(R.layout.activity_add_less)
public class AddLessActivity extends BaseActivity {
    @ViewInject(R.id.edit_less_name)
    EditText editName;
    @ViewInject(R.id.edit_less_chapter)
    EditText editChapter;
    @ViewInject(R.id.edit_less_content)
    EditText editContent;
    @ViewInject(R.id.txt_less_time)
    TextView txtLessTime;
    @ViewInject(R.id.txt_nav_title)
    TextView txtTitle;
    @ViewInject(R.id.txt_less_close)
    TextView txtLessClose;
    @ViewInject(R.id.line_edit)
    LinearLayout lineEdit;

    private UpdateTimeBroadcast updateTimeBroadcast;
    private Less less;
    private List<Less> allLess;
    private int editFlag = -1;

    @Override
    public void initViews() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Comm.ACTION_UPDATE_ADD_TIME);
        updateTimeBroadcast = new UpdateTimeBroadcast();
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(updateTimeBroadcast, filter);
    }

    @Override
    public void initDatas() {
        allLess = new ArrayList<>();
        if (!TextUtils.isEmpty(userPreference.getString("allLess", ""))) {
            allLess = GsonUtils.LessGsonToBean(userPreference.getString("allLess", ""));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        less = getIntent().getParcelableExtra("less");
        if (null != less) {
            txtTitle.setText("课程记录");
            editName.setEnabled(false);
            editChapter.setEnabled(false);
            editContent.setEnabled(false);
            txtLessTime.setEnabled(false);
            lineEdit.setVisibility(View.VISIBLE);
            editName.setText(less.getName());
            editChapter.setText(less.getChapter());
            editContent.setText(less.getContent());
            txtLessTime.setText(less.getTime());
        }
    }

    @Event(value = {R.id.txt_less_close, R.id.txt_less_yes,
            R.id.txt_less_time, R.id.txt_edit, R.id.txt_delete}, type = View.OnClickListener.class)
    private void OnClick(View view) {
        switch (view.getId()) {
            case R.id.txt_less_close:
                finish();
                overridePendingTransition(R.anim.push_right_in,
                        R.anim.push_right_out);
                break;
            case R.id.txt_less_yes:
                if (null == less || editFlag == 1)
                    addLess();
                break;
            case R.id.txt_less_time:
                Intent intent = new Intent(AddLessActivity.this, DateTimeActivity.class);
                startActivity(intent);
                break;
            case R.id.txt_edit:
                editName.setEnabled(true);
                editChapter.setEnabled(true);
                editContent.setEnabled(true);
                txtLessTime.setEnabled(true);

                for (Less item : allLess) {
                    if (item.getTime().equals(less.getTime())) {
                        allLess.remove(item);
                        break;
                    }
                }
                editFlag = 1;
                break;
            case R.id.txt_delete:

                for (Less item : allLess) {
                    if (item.getTime().equals(less.getTime())) {
                        allLess.remove(item);
                        //保存备课数据
                        userPreference.edit().putString("allLess", GsonUtils.GsonString(allLess)).apply();
                        finish();
                        overridePendingTransition(R.anim.push_right_in,
                                R.anim.push_right_out);
                        return;
                    }
                }
                break;
        }
    }

    private void addLess() {
        Log.w("LESS","ADDlESS");
        if (TextUtils.isEmpty(editName.getText().toString())) {
            Toast.makeText(this, "课程名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(editChapter.getText().toString())) {
            Toast.makeText(this, "章节不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(editContent.getText().toString())) {
            Toast.makeText(this, "备课内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        allLess.add(new Less(editName.getText().toString(), txtLessTime.getText().toString(),
                editChapter.getText().toString(), editContent.getText().toString()));
        //保存备课数据
        userPreference.edit().putString("allLess", GsonUtils.GsonString(allLess)).apply();

        List<ScheBean> scheDatas = new ArrayList<>();
        if (!TextUtils.isEmpty(userPreference.getString("allSche", ""))) {
            scheDatas = GsonUtils.ScheGsonToBean(userPreference.getString("allSche", ""));
        }
        //保存所有日程数据
        scheDatas.add(new ScheBean(editName.getText().toString(), "", txtLessTime.getText().toString(), txtLessTime.getText().toString(), editContent.getText().toString(), false));
        userPreference.edit().putString("allSche", GsonUtils.GsonString(scheDatas)).apply();

        Intent intent = new Intent();
        intent.setAction(Comm.ACTION_UPDATE_ADD_LESS);
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

        Intent intent1 = new Intent();
        intent1.setAction(Comm.ACTION_UPDATE_ADD_SCHE);
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent1);
        finish();
        overridePendingTransition(R.anim.push_right_in,
                R.anim.push_right_out);
    }

    private class UpdateTimeBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Comm.ACTION_UPDATE_ADD_TIME:
                    txtLessTime.setText(intent.getStringExtra("year") + "/" + intent.getStringExtra("month") + "/" +
                            intent.getStringExtra("day") + " " + FormatUtils.formatWeek(intent.getIntExtra("week", 0) - 1) + " " +
                            intent.getStringExtra("hour") + ":" + intent.getStringExtra("minute"));
                    break;
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null == updateTimeBroadcast)
            LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(updateTimeBroadcast);
    }
}
